import Vue from 'vue'
// 七牛云上传
const qiniu = require('qiniu-js')

const config = {
  useCdnDomain: true,
  region: qiniu.region.z2
}

export function uploadToQiniu (file, key, observer, putExtra) {
  // 先获取七牛上传凭证
  Vue.prototype.$api.getUploadToken().then(res => {
    const observable = qiniu.upload(file, key, res.data, putExtra, config)
    observable.subscribe(observer) // 上传开始
  }).catch(error => observer.error(error))
}
